/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.AllsizeTechnology;
import me.zhengjie.domain.FzAllSize;
import me.zhengjie.domain.FzSamples;
import me.zhengjie.domain.SampleTechnology;
import me.zhengjie.repository.FzAllSizeRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AllsizeTechnologyRepository;
import me.zhengjie.service.AllsizeTechnologyService;
import me.zhengjie.service.dto.AllsizeTechnologyDto;
import me.zhengjie.service.dto.AllsizeTechnologyQueryCriteria;
import me.zhengjie.service.mapstruct.AllsizeTechnologyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zcc
* @date 2021-07-15
**/
@Service
@RequiredArgsConstructor
public class AllsizeTechnologyServiceImpl implements AllsizeTechnologyService {

    private final AllsizeTechnologyRepository allsizeTechnologyRepository;
    private final AllsizeTechnologyMapper allsizeTechnologyMapper;
    private final FzAllSizeRepository fzAllSizeRepository;

    @Override
    public Map<String,Object> queryAll(AllsizeTechnologyQueryCriteria criteria, Pageable pageable){
        Page<AllsizeTechnology> page = allsizeTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(allsizeTechnologyMapper::toDto));
    }

    @Override
    public List<AllsizeTechnologyDto> queryAll(AllsizeTechnologyQueryCriteria criteria){
        return allsizeTechnologyMapper.toDto(allsizeTechnologyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AllsizeTechnologyDto findById(String techId) {
        AllsizeTechnology allsizeTechnology = allsizeTechnologyRepository.findById(techId).orElseGet(AllsizeTechnology::new);
        ValidationUtil.isNull(allsizeTechnology.getTechId(),"AllsizeTechnology","techId",techId);
        return allsizeTechnologyMapper.toDto(allsizeTechnology);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AllsizeTechnologyDto create(AllsizeTechnology resources) {
        String techid = IdUtil.simpleUUID();
        resources.setTechId(techid);
        AllsizeTechnology allsizeTechnology = allsizeTechnologyRepository.save(resources);

        FzAllSize allSize = fzAllSizeRepository.findById(allsizeTechnology.getAsId()).orElseGet(FzAllSize::new);
        List<AllsizeTechnology> technologies = allsizeTechnologyRepository.findByAsId(allsizeTechnology.getAsId());

        allSize.setAllsizeTechnologyId(technologies.stream().map(AllsizeTechnology::getTechId).collect(Collectors.joining(",")));
        fzAllSizeRepository.save(allSize);

        return allsizeTechnologyMapper.toDto(allsizeTechnology);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AllsizeTechnology resources) {
        AllsizeTechnology allsizeTechnology = allsizeTechnologyRepository.findById(resources.getTechId()).orElseGet(AllsizeTechnology::new);
        ValidationUtil.isNull( allsizeTechnology.getTechId(),"AllsizeTechnology","id",resources.getTechId());
        allsizeTechnology.copy(resources);
        allsizeTechnologyRepository.save(allsizeTechnology);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String techId : ids) {
            AllsizeTechnology allsizeTechnology =  allsizeTechnologyRepository.findById(techId).orElseGet(AllsizeTechnology::new);
            allsizeTechnologyRepository.deleteById(techId);

            FzAllSize allSize = fzAllSizeRepository.findById(allsizeTechnology.getAsId()).orElseGet(FzAllSize::new);
            List<AllsizeTechnology> technologies = allsizeTechnologyRepository.findByAsId(allSize.getAsId());

            allSize.setAllsizeTechnologyId(technologies.stream().map(AllsizeTechnology::getTechId).collect(Collectors.joining(",")));
            fzAllSizeRepository.save(allSize);
        }
    }

    @Override
    public void download(List<AllsizeTechnologyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AllsizeTechnologyDto allsizeTechnology : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" asId",  allsizeTechnology.getAsId());
            map.put(" color",  allsizeTechnology.getColor());
            map.put(" tech1",  allsizeTechnology.getTech1());
            map.put(" tech2",  allsizeTechnology.getTech2());
            map.put(" tech3",  allsizeTechnology.getTech3());
            map.put(" tech4",  allsizeTechnology.getTech4());
            map.put(" tech5",  allsizeTechnology.getTech5());
            map.put(" tech6",  allsizeTechnology.getTech6());
            map.put(" tech7",  allsizeTechnology.getTech7());
            map.put(" tech8",  allsizeTechnology.getTech8());
            map.put(" tech9",  allsizeTechnology.getTech9());
            map.put(" tech10",  allsizeTechnology.getTech10());
            map.put(" tech11",  allsizeTechnology.getTech11());
            map.put(" tech12",  allsizeTechnology.getTech12());
            map.put(" tech13",  allsizeTechnology.getTech13());
            map.put(" tech14",  allsizeTechnology.getTech14());
            map.put(" tech15",  allsizeTechnology.getTech15());
            map.put(" tech16",  allsizeTechnology.getTech16());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}